| 
                
               | 
              
                
               | 
              
                @@ -34,11 +34,18 @@ def connect_mqtt(): 
               | 
            
            
            
              | 
                34
               | 
              
                34
               | 
              
                             print('Connected to MQTT Broker')
               | 
            
            
            
              | 
                35
               | 
              
                35
               | 
              
                         else: 
               | 
            
            
            
              | 
                36
               | 
              
                36
               | 
              
                             print('Failed to connect, return code %d\n', rc)
               | 
            
            
            
              | 
                
               | 
              
                37
               | 
              
                + 
               | 
            
            
            
              | 
                
               | 
              
                38
               | 
              
                +    def on_disconnect(client, userdata, rc): 
               | 
            
            
            
              | 
                
               | 
              
                39
               | 
              
                +        if rc != 0: 
               | 
            
            
            
              | 
                
               | 
              
                40
               | 
              
                +            print("Unexpected MQTT disconnection. Will auto-reconnect")
               | 
            
            
            
              | 
                
               | 
              
                41
               | 
              
                + 
               | 
            
            
            
              | 
                37
               | 
              
                42
               | 
              
                     # Set Connecting Client ID 
               | 
            
            
            
              | 
                38
               | 
              
                43
               | 
              
                     print(f'Connected to MQTT Broker by client_id `{client_id}`')
               | 
            
            
            
              | 
                39
               | 
              
                
               | 
              
                -    client = mqtt_client.Client(client_id) 
               | 
            
            
            
              | 
                
               | 
              
                44
               | 
              
                +    # https://github.com/eclipse/paho.mqtt.python/issues/573 
               | 
            
            
            
              | 
                
               | 
              
                45
               | 
              
                +    client = mqtt_client.Client(client_id, clean_session=False) 
               | 
            
            
            
              | 
                40
               | 
              
                46
               | 
              
                     client.username_pw_set(username, password=password) 
               | 
            
            
            
              | 
                41
               | 
              
                47
               | 
              
                     client.on_connect = on_connect 
               | 
            
            
            
              | 
                
               | 
              
                48
               | 
              
                +    client.on_disconnect = on_disconnect 
               | 
            
            
            
              | 
                42
               | 
              
                49
               | 
              
                     client.connect(broker, port) 
               | 
            
            
            
              | 
                43
               | 
              
                50
               | 
              
                     return client 
               | 
            
            
            
              | 
                44
               | 
              
                51
               | 
              
                  
               |